<?php
namespace api\user\controller;

use api\common\controller\RestBaseController;
use think\Validate;
use think\View;

class CodeController extends RestBaseController
{
    public function send()
    {
        $validate = new Validate([
            'mobile' => 'require',
        ]);

        $validate->message([
            'mobile.require' => '请输入手机号',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }


        $accountType = '';

       if (cmf_check_mobile($data['mobile'])) {
            $accountType = 'mobile';
        } else {
            $this->error("请输入正确的手机");
        }

        //TODO 限制 每个ip 的发送次数

        $code = cmf_get_verification_code($data['mobile']);
        if (empty($code)) {
            $this->error("验证码发送过多,请明天再试!");
        }

        if ($accountType == 'mobile') {

            $apikey ='dc9348c09988e2699dee00c198b33fd2';
            //$text = "【星际空间】您的验证码是".$code."。如非本人操作，请忽略本短信";
            $text = "【MINEEX】您的手机验证码是".$code;
            $result = send_code($data['mobile'],$text);
            if ($result == false || $result['code']!= 0) {
                $this->error('发送失败');
            }
            cmf_verification_code_log($data['mobile'], $code);
            $this->success('验证码已经发送成功!');

        }


    }

}
